function viewMyOrders(flag)
{
    if(flag == 0)
    {
        window.location.href = "/order/view/?peopleId=" + $("#peopleId").val() + "&peopleName=" + $("#peopleId").find("option:selected:first").text();
    }
    else if(flag == 1)
    {
        window.location.href = "/order/view/?peopleId=all";
    }
    else if(flag == 2)
    {
        window.location.href = "/order/view/?peopleId=today";
    }
}

function showDialog(obj)
{
    var row = $(obj).parents("tr:first").find("td");

    var dialogRow = $("#tclist").find("tr");

     $("#hdnOrderId").val($(row[0]).text());
     $(dialogRow[0]).find("td:last").text($(row[1]).text());
     $(dialogRow[1]).find("td:last").text($(row[2]).text());
     $(dialogRow[2]).find("td:last").text($(row[3]).text());
     $(dialogRow[3]).find("td:last").text($(row[4]).text() + " * " + $(row[5]).text() + " = " + parseFloat($(row[4]).text())* parseFloat($(row[5]).text()));
     $(dialogRow[4]).find("td:last").text($(row[7]).text());
     $(dialogRow[5]).find("td:last").text($(row[8]).text());
     $(dialogRow[6]).find("td:last").text($(row[9]).text());
     
     $("#tctitle").text($(row[1]).text() + "的定餐记录");

     $("#tc").css("left", parseInt($(document).width()/2 - $("#tc").width()/2).toString() + "px");
     $("#tc").css("top", parseInt($(document.documentElement).attr("clientHeight") / 2 - $("#tc").height() / 2 + $(document.documentElement).attr("scrollTop")).toString() + "px");
     $("#tc").show();
}

function closeDialog()
{
    $("#tc").hide();
}

function pay()
{
    window.location.href = "/order/pay/?orderId=" + $("#hdnOrderId").val() + "&peopleId=" + $("#hdnPeopleId").val();
}

function del()
{
    window.location.href = "/order/delete/?orderId=" + $("#hdnOrderId").val() + "&peopleId=" + $("#hdnPeopleId").val();
}

function changeFoods(obj)
{
    var shopId = $(obj).val();

    $("#note").text($("#shopId").find("option:selected:first").attr("note"));

    var foodObj = $("#foodId");
    foodObj.empty();
    foodObj.append("<option>Loading...</option>");
    foodObj.attr("disabled", "disabled");
    $.getJSON('/order/getfoodsbyshop/', {'shopId': shopId, 'rnd': Math.random()}, function(data,b)
    {
        foodObj.empty();
        for(var i = 0; i < data.length; i++)
        {
            foodObj.append("<option value='" + data[i].id + "'>" + data[i].name + " - " + data[i].price + "</option>")
        }

        foodObj.removeAttr("disabled");
    });
}

function changePeople(obj)
{
    $("#ajaxLoader").show();
    obj = $(obj);

    $("#unpaidPeople").text(obj.find("option:selected:first").text());
    var peopleId = obj.val();

    var unpaidList = $("#unpaidlist tbody");

    var totalPrice = 0;

    $.getJSON('/order/getUnpaidOrder/', {'peopleId': peopleId, 'rnd': Math.random()}, function(data,b)
    {
        unpaidList.empty();
        for(var i = 0; i < data.length; i++)
        {
            var subTotal = parseFloat(data[i].Food.price) * parseInt(data[i].Order.count);
            totalPrice += subTotal;
            unpaidList.append("<tr><td>" + data[i].Shop.name + "</td><td>" + data[i].Food.name + "</td><td>" + data[i].Order.count + "</td><td>" + subTotal + "</td><td>" + $.PHPDate("Y-m-d", new Date(data[i].Order.created.replace(/-/g, "/"))) + "</td></tr>");
        }

        $("#unpaidlist tfoot tr td").text("合计: " + totalPrice);

        $("#ajaxLoader").hide();
    });
}

$(function()
{
    $("#note").text($("#shopId").find("option:selected:first").attr("note"));
    changePeople($("#peopleId")[0]);
    $("#btnOrder").hover(function()
        {
            $(this).attr("src", "/img/order_btn_active.png");
        }, function()
        {
            $(this).attr("src", "/img/order_btn.png");
        });
    $("#btnClose").hover(function()
        {
            $(this).attr("src", "/img/close_btn_active.png");
        }, function()
        {
            $(this).attr("src", "/img/close_btn.png");
        });

});
